<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\NewsModel;
use Common\Model\NewsRecommendModel;
use Common\Model\NewsVideoModel;
use Think\Cache\Driver\Redis;
use Think\Log;

class NewsVideoController extends AdminbaseController{

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 发布视频页面
     */
    public function index(){
        $column_id = I("column_id")? I("column_id") : '';
        $area_id   = I("area_id") ? I("area_id") : '';
		//返回 老版本 1，前端交互0 ，默认0
		$return_val = I("return_val") ? I("return_val") : 0;
        $id_type = '';
        $id_val  = '';
        if($column_id){
            $id_type = "column_id";
            $id_val  = $column_id;
        }else{
            $id_type = "area_id";
            $id_val  = $area_id;
        }
        $news_id = I("news_id");
        $type = I("type",'1'); //默认为1,新闻列表，2时为专题下的新闻列表
        $type_id = I("type_id",''); //对应的专题分栏的id
        $special_id = I("special_id"); //专题id
        if(!$id_type){
            $this->error("查找新闻失败！");
        }
        if($type == '2'){
        	$special_column_type = M("vrnews1.column_type",'special_');
        	$special_column_list = $special_column_type->where('status = 1 and news_id = '.$special_id)->field('type_id,name')->select();
        	$this->assign("type", $type);
        	$this->assign("type_id", $type_id);
        	$this->assign("special_id", $special_id);
        	$this->assign("special_column_type_list", $special_column_list);
        }else{
            $column_type_list = array();
            if($column_id) {
                $column_type = M("vrnews1.column_type", 'news_');
                $column_type_list = $column_type->where(array('column_id' => $column_id, 'status' => 1))->field("type_id, name")->select();
                $this->assign("type_list", $column_type_list);
            }
        }
        
        $news = M("vrnews1.news", NULL);
        $news_info = $news->join("vrnews1.news_video on news_video.news_id = news.news_id",'left')
            ->where(array('news.news_id'=>$news_id))->find();
        
        $relation = new NewsRecommendModel();
        $news_info['relation'] = $relation->getNewsRelation($news_id);
        $this->assign("news_info", $news_info);
        $this->assign("type_list", $column_type_list);
        $this->assign("id_type", $id_type);
        $this->assign("id_val", $id_val);
        $this->assign("news_id", $news_id);
		$this->assign("return_val",$return_val);
        $this->display();
    }

    /**
     * 添加视频新闻
     */
    public function add_videoNews(){
    	//接受手填视频信息
    	$file_id =  I("file_id");
    	$qcloud_file_name =  I("qcloud_file_name");
    	
        $news_id          = I("news_id");
        $data['title']    = I("post.title");
        $data['subtitle'] = I("post.subTitle");
        $data['keywords'] = I("post.keyword");
        $source           = I("post.source");
        $data['weight']    = (int)(I("post.weight") ? I("post.weight") :'70');
        $data['tags']     = I("post.tags");
        $data['abstract'] = I("post.abstract");
        $data['top_status']  = I("post.top_status");
        $is_vr            = I("post.is_vr",0);
        $data['special_news_id']  = !empty(I("post.special_id")) ? I("post.special_id") : '0';
        $data['type']     = 4;
        $data['type_id']     = !empty(I("post.column_type")) ? I("post.column_type") : (I("post.special_type") ? I("post.special_type") : 0);
        $pub                 = I("post.pub");
        $reference           = I("post.reference");
        $data['outer_url']   = I("post.outer_url");
        $outer_url_pub    = I("post.outer_url_pub");
        $fileName    = I("post.fileName");
        $fileSize    = I("post.fileSize");
        $fileDir    = I("post.fileDir");
        
        if($outer_url_pub == '1'){
        	if(empty($data['outer_url'])){
        		return $this->error("无外链地址！");
        	}
        	$data['outer_url_ishot'] = '1';
        }else{
        	$data['outer_url_ishot'] = '0';
        }
        $data['is_watermark']      = I("post.water");
        $news_recommend      = I("post.news_recommend");
        $data['source_name'] = $source;
        $data['column_id']   = I("column_id") ? I("column_id") : '';
        $data['area_id']     = I("area_id") ? I("area_id") : "";
        $id_type = '';
        $id_val  = '';
        if(I("column_id")){
            $id_type = "column_id";
            $id_val  = I("column_id");
        }else{
            $id_type = "area_id";
            $id_val  = I("area_id");
        }
        if($news_id){
            $data['news_id'] = $news_id;
        }else{
            $data['news_id'] = time().$this->getRange();
        }

        if(mb_strlen(trim($data['subtitle']),'UTF8') > 10){
        	return $this->error("副标题不能超过10个字");
        }
        
        if (!preg_match('/^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/', $data['weight'])) {
            return $this->error("权重必须为0到255的数字！");
        }

        if((!empty($fileName) && !empty($fileSize)) && !$file_id && !$qcloud_file_name){
        	$video['file_name'] = $fileName;
        	$newsModel = new NewsVideoModel();
        	$result    = $newsModel->uploadVideo($fileName, $fileSize, $fileDir, $data['news_id'],$is_vr); //上传视频
        }
        if(!empty($_FILES['imageFile']['name'])){
            $newsModel = new NewsModel();
            $imageUrl  = $newsModel->uploadImage();  //上传封面图
            $data['cover_image'] = $imageUrl;
        }

        if(!empty($source)){
	        $sourceObj   = M("vrnews1.news_source", NULL);
	        $source_item = $sourceObj->where(array('name' => $source))->find();
	        if(empty($source_item)){  //查询来源是否存在，不存在加一条
	            $sourcelist['name'] = $source;
	            $sourcelist['create_time'] = date('Y-m-d H:i:s', time());
	            $sourcelist['creator_id']  = $_SESSION['ADMIN_ID'];
	            $sourceObj->add($sourcelist);
	            $data['source_id'] = $sourceObj->getLastInsID();
	        }else{
	            $data['source_id'] = $source_item['source_id'];
	        }
        }

        if(!empty($pub)){
            if(in_array(1, $pub)){
    			$data['app_pub'] = 1;
    		}else{
    			$data['app_pub'] = 0;
    		}
    		if(in_array(2, $pub)){
    			$data['web_pub'] = 1;
    		}else{
    			$data['web_pub'] = 0;
    		}
        }
        
        if(!empty($reference)){
            /* $id   = substr($reference, 3);
            $type = substr($reference, strrpos($reference, '=')+1); */
            
            parse_str($_POST['reference']);
            
            $data['reference_id']   = $id;
            $data['reference_type'] = $type;
			$reference_type = $type;
        }
        
        $news = M("vrnews1.news", NULL);
		$column_type = M("vrnews1.news_column",null);
		$video_column = $column_type->where("name = '视频'")->field('column_id')->find();
        if($news_id){
            $news->where(array('news_id'=>$news_id))->save($data);
        }else{
        	/* 复制视频新闻 */
        	if($reference_type == 2){
        		$_news_data = $news->where(array('news_id'=>$id))->find();
        		if(!$_news_data){$this->error('找不到复制的新闻，请检查ID');exit();}
        		if($_news_data['type'] != 4){$this->error('请复制视频新闻的ID');exit();}
        		$data['title']    		  = $_news_data['title'];
        		$data['subtitle'] 		  = $_news_data['subtitle'];
        		$data['keywords']		  = $_news_data['keywords'];
        		$data['source_id']		  = $_news_data['source_id'];
        		$data['source_name'] 	  = $_news_data['source_name'];
        		$data['tags'] 			  = $_news_data['tags'];
        		$data['abstract'] 		  = $_news_data['abstract'];
        		$data['cover_image'] 	  = $_news_data['cover_image'];
        		$data['content'] 		  = $_news_data['content'];
        		$data['outer_url'] 		  = $_news_data['outer_url'];
        		$data['outer_url_ishot']  = $_news_data['outer_url_ishot'];
        		$data['app_pub'] 		  = $_news_data['app_pub'];
        		$data['web_pub'] 	 	  = $_news_data['web_pub'];
        		//$data['is_watermark'] = $_news_data['is_watermark'];
        		$data['weight'] 		  = $_news_data['weight'];
        		$data['top_status'] 	  = $_news_data['top_status'];
        		$data['full_status'] 	  = $_news_data['full_status'];
        		$data['full_title'] 	  = $_news_data['full_title'];
        		$data['full_subtitle']	  = $_news_data['full_subtitle'];
        		$data['full_cover_image'] = $_news_data['full_cover_image'];
        		$data['app_pub'] 		  = $_news_data['app_pub'];
        		//$data['status'] 		  = $_news_data['status'];
        		
        		$data['reference_id']	  = 0;
        		$data['reference_type']   = 0;
        	}
        	/* 复制视频新闻 */
        	
        	$data['create_time'] = date('Y-m-d H:i:s', time());
        	$data['refresh_time'] = time();
            $news->add($data);
			if(($reference_type != 2 && $reference_type != 1 && $data['column_id'] != $video_column['column_id']) || ($data['special_news_id'] != 0 && $reference_type != 2)){
				$video_data = $data;
				$video_data['news_id']   = time().$this->getRange();
				$video_data['column_id'] = $video_column['column_id'];
				$video_data['area_id']   = '';
				$video_data['type_id']   = '';
				$video_data['special_news_id'] = 0;
				$news->add($video_data);
			}
        }
        
        if($is_vr == 0){
        	$is_vr = 1;
        }else {
        	$is_vr = 2;
        }
        if($file_id && $qcloud_file_name){
        	$newsModel = new NewsVideoModel();
        	$video_list_data = $newsModel->get_video($qcloud_file_name);
        	if(!empty($video_list_data)){
        		if($video_list_data['codeDesc'] == 'Success'){
        			foreach($video_list_data['fileSet'] as $key=>$value){
        				if($value['fileId'] == $file_id){
        					foreach($value['playSet'] as $key1=>$value1){
//         						if($is_vr == '2'){
//         							if($value1['definition'] == '0'){
//         								$update_data['video_url'] = $value1['url'];
//         								$update_data['height'] = $value1['vheight'];
//         								$update_data['width'] = $value1['vwidth'];
//         								$update_data['size'] = '0';
//         							}
//         						}else{
        							if($value1['definition'] == '30'){
        								$update_data['video_url'] = $value1['url'];
        								$update_data['height'] = $value1['vheight'];
        								$update_data['width'] = $value1['vwidth'];
        								$update_data['size'] = '0';
        							}
//         						}		 
        						if($value1['definition'] == '20'){
        							$update_data['video_url1'] = $value1['url'];
        							$update_data['height1'] = $value1['vheight'];
        							$update_data['width1'] = $value1['vwidth'];
        							$update_data['size1'] = '0';
        						}
        						 
        						if($value1['definition'] == '10'){
        							$update_data['video_url2'] = $value1['url'];
        							$update_data['height2'] = $value1['vheight'];
        							$update_data['width2'] = $value1['vwidth'];
        							$update_data['size2'] = '0';
        						}
        						 
        					}
        					$update_data['thumbnail_url'] = $value['image_url'];
        				}
        			}
        			if(!empty($update_data)){
        				$update_data['status'] = '2';
        			}else{
        				$update_data['status'] = '3';
        			}
        	
        			$update_data['duration'] = $this->changeTimeType($value['duration']);
        			$update_data['category'] = $is_vr;
        			$update_data['update_time'] = date('Y-m-d H:i:s',time());
        			$update_data['news_id'] = $data['news_id'];
        			$update_data['file_name'] = $qcloud_file_name;
        			$update_data['file_id'] = $file_id;
        			$update_data['text'] = json_encode($video_list_data);
        			 
        		}
        	}
        	if($update_data){
        		$videoObj = new NewsVideoModel();
        		$videoObj->delete($data['news_id']);
        		$videoObj->add($update_data);
				if((!$news_id && $reference_type != 2 && $reference_type != 1 && $data['column_id'] != $video_column['column_id']) || ($data['special_news_id'] != 0 && $reference_type != 2)){
					$update_data['news_id'] = $video_data['news_id'];
					$videoObj->add($update_data);
				}
        	}
        }else{
        	/* 引用的视频不能保存在news_video表中，所以点击数不增加 */
        	//if(!empty($result)){  //添加视频
        		$videoObj = new NewsVideoModel();
        		
        		$video['news_id'] = $data['news_id'];
        		$video['file_id'] = $result;
        		$video['status'] = '1';
        		$video['category']= $is_vr;
        		
        		if($reference_type == 2){
        			$_news_video_data = $videoObj->where(array('news_id'=>$id))->find();
        			if(!$_news_video_data){$this->error('没有找到该视频，请检查ID');exit();}
        			$video['thumbnail_url'] = $_news_video_data['thumbnail_url'];
        			$video['file_name'] = $_news_video_data['file_name'];
        			$video['file_id'] = $_news_video_data['file_id'];
        			$video['video_url'] = $_news_video_data['video_url'];
        			$video['video_url1'] = $_news_video_data['video_url1'];
        			$video['video_url2'] = $_news_video_data['video_url2'];
        			$video['duration'] = $_news_video_data['duration'];
        			$video['height'] = $_news_video_data['height'];
        			$video['height1'] = $_news_video_data['height1'];
        			$video['height2'] = $_news_video_data['height2'];
        			$video['width'] = $_news_video_data['width'];
        			$video['width1'] = $_news_video_data['width1'];
        			$video['width2'] = $_news_video_data['width2'];
        			$video['size'] = $_news_video_data['size'];
        			$video['size1'] = $_news_video_data['size1'];
        			$video['size2'] = $_news_video_data['size2'];
        			$video['category'] = $_news_video_data['category'];
        			//$update_data['play_count'] = $_news_video_data['play_count'];
        			$video['text'] = $_news_video_data['text'];
        			$video['status'] = $_news_video_data['status'];
        		}
        		
        		//删除视频
        		$videoObj->delete($data['news_id']);
        		$videoObj->add($video);
				if((!$news_id && $reference_type != 2 && $reference_type != 1 && $data['column_id'] != $video_column['column_id']) || ($data['special_news_id'] != 0 && $reference_type != 2)){
					$video['news_id'] = $video_data['news_id'];
					$videoObj->add($video);
				}
        	//}
        }
        if(!empty($news_recommend)){  //相关新闻
            $recommendObj = M("vrnews1.news_recommend", NULL);
            $recommendObj->where(array('news_id' => $data['news_id']))->delete();
            /* for($i=0; $i<count($news_recommend); $i++ ){
                if(!empty($news_recommend[$i])){
                    $id   = substr($news_recommend[$i], 3);
                    $reference_id = substr($id,0, strpos($id, '&'));
                    $type = substr($news_recommend[$i], strrpos($news_recommend[$i], '=')+1);

                    $dataList[] = array('news_id'=>$data['news_id'], 'recommend_id'=>$reference_id, 'type' => $type);
                }
            } */
            
            $dataList = array();
            foreach($_POST['news_recommend'] as $list){
            	if($list && strpos($list, '=')){
            		parse_str($list, $_param);
            		$_param['news_id'] = $data['news_id']  ? $data['news_id'] : $news_id;
            		$_param['recommend_id'] = $_param['id'];
            		unset($_param['id']);
            		$dataList[] = $_param;
            	}
            }
            if(!empty($dataList)){
                $recommendObj->addAll($dataList);
            }
			if((!$news_id && $reference_type != 2 && $reference_type != 1 && $data['column_id'] != $video_column['column_id']) || ($data['special_news_id'] != 0 && $reference_type != 2)){
				foreach($_POST['news_recommend'] as $list){
					if($list && strpos($list, '=')){
						parse_str($list, $_param);
						$_param['news_id'] = $video_data['news_id'];
						$_param['recommend_id'] = $_param['id'];
						unset($_param['id']);
						$dataList_v[] = $_param;
					}
				}
				if(!empty($dataList_v) && count($dataList_v) > 0){
					$recommendObj->addAll($dataList_v);
				}
			}

        }
		$redis = new Redis();
		if($id_type == 'column_id'){
			$column_ids = $data['column_id'];
			$is_area = 0;
		}else if($id_type == 'area_id'){
			$column_ids  = I("area_id");
			$is_area = 1;
		}
		if($data['special_news_id'] != 0){
			$name = C('environment')."_special_list_" . $data['special_news_id'] . '_update';
		}else {
			$name = C('environment').'_new_list_' . $is_area . '_' . $column_ids . '_update';
		}
		$key = $redis->buildKey($name);
		$redis->set_redis($key, time());
        if($data['special_news_id'] !='0'){
//            $this->success('发布成功！', U("Admin/News/index",array($id_type=>$id_val)));
        	$this->success('发布成功！', U("Admin/Special/index",array($id_type=>$id_val,'news_id'=>$data['special_news_id'])));
        }else{
        	$this->success('发布成功！', U("Admin/News/index?".$id_type."=".$id_val));
        }
    }
    
    /**
     * 打开上传视频页面
     * 
     */
    public function open_upload(){
    	$this->assign("Title", "上传视频");
    	$this->display('open_upload');
    }
    
    public function test(){
    	$this->assign("Title", "上传视频");
    	$this->display('test');
    }
    
    
    /**
     * 分片上传文件
     * @return string|unknown|boolean
     */
    public function upload_video(){
    	$REQUEST_METHOD=$_SERVER['REQUEST_METHOD'];
    	global $uploads_dir;

    	$fileDir = $_REQUEST['resumableFileDir'];
    	$uploads_dir="data/upload/".$fileDir;
    	if($REQUEST_METHOD == "GET")
    	{
    		if(count($_GET)>0)
    		{
    			$chunkNumber = $_GET['resumableChunkNumber'];
    			$chunkSize = $_GET['resumableChunkSize'];
    			$totalSize = $_GET['resumableTotalSize'];
    			$identifier = $_GET['resumableIdentifier'];
    			$filename = iconv ( 'UTF-8', 'GB2312', $_GET ['resumableFilename'] );
    			if($this->validateRequest($chunkNumber, $chunkSize, $totalSize, $identifier, $filename)=='valid')
    			{
    				$chunkFilename = $this->getChunkFilename($chunkNumber, $identifier,$filename);
    				{
    					if(file_exists($chunkFilename)){
    						echo "found";
    					} else {
    						header("HTTP/1.0 404 Not Found");
    						echo "not_found";
    					}
    				}
    			}
    			else
    			{
    				header("HTTP/1.0 404 Not Found");
    				echo "not_found";
    			}}
    	}

    	if($REQUEST_METHOD == "POST"){
    		if(count($_POST)>0)
    		{
    			$resumableFilename = iconv ( 'UTF-8', 'GB2312', $_POST ['resumableFilename'] );//$_POST ['resumableFilename'];//
    			$resumableIdentifier=$_POST['resumableIdentifier'];
    			$resumableChunkNumber=$_POST['resumableChunkNumber'];
    			$resumableTotalSize=$_POST['resumableTotalSize'];
    			$resumableChunkSize=$_POST['resumableChunkSize'];
    			if (!empty($_FILES)) foreach ($_FILES as $file) {
    				// check the error status
    				if ($file['error'] != 0) {
    					$this->_log('error '.$file['error'].' in file '.$resumableFilename);
    					continue;
    				}
    				// init the destination file (format <filename.ext>.part<#chunk>
    				// the file is stored in a temporary directory
    	
    				global $uploads_dir;

    				$temp_dir = $uploads_dir.'/'.$resumableIdentifier;
    				$dest_file = $temp_dir.'/'.$resumableFilename.'.part'.$resumableChunkNumber;
    				// create the temporary directory
    				if (!is_dir($temp_dir)) {
    					mkdir($temp_dir, 0777, true);
    				}
    				// move the temporary file
    				if (!move_uploaded_file($file['tmp_name'], $dest_file)) {
    					$this->_log('Error saving (move_uploaded_file) chunk '.$resumableChunkNumber.' for file '.$resumableFilename);
    				} else {
    					// check if all the parts present, and create the final destination file
    					$this->createFileFromChunks($temp_dir, $resumableFilename,$resumableChunkSize, $resumableTotalSize);
    				}
    			}
    		}
    	}
    }
    
    
    function cleanIdentifier ($identifier){
    	return $identifier;
    }
    
    
    function validateRequest ($chunkNumber, $chunkSize, $totalSize, $identifier, $filename, $fileSize=''){
    	// Clean up the identifier
    	//$identifier = cleanIdentifier($identifier);
    	// Check if the request is sane
    	if ($chunkNumber==0 || $chunkSize==0 || $totalSize==0 || $identifier==0 || $filename=="") {
    		return 'non_resumable_request';
    	}
    	$numberOfChunks = max(floor($totalSize/($chunkSize*1.0)), 1);
    	if ($chunkNumber>$numberOfChunks) {
    		return 'invalid_resumable_request1';
    	}
    	// Is the file too big?
    	//      if($maxFileSize && $totalSize>$maxFileSize) {
    	//          return 'invalid_resumable_request2';
    	//      }
    	if($fileSize!="") {
    		if($chunkNumber<$numberOfChunks && $fileSize!=$chunkSize) {
    			// The chunk in the POST request isn't the correct size
    			return 'invalid_resumable_request3';
    		}
    		if($numberOfChunks>1 && $chunkNumber==$numberOfChunks && $fileSize!=(($totalSize%$chunkSize)+$chunkSize)) {
    			// The chunks in the POST is the last one, and the fil is not the correct size
    			return 'invalid_resumable_request4';
    		}
    		if($numberOfChunks==1 && $fileSize!=$totalSize) {
    			// The file is only a single chunk, and the data size does not fit
    			return 'invalid_resumable_request5';
    		}
    	}
    	return 'valid';
    }
    
    
    /**
     *
     * Logging operation - to a file (upload_log.txt) and to the stdout
     * @param string $str - the logging string
     */
    function _log($str) {
    	// log to the output
    	$log_str = date('d.m.Y').": {$str}\r\n";
    	echo $log_str;
    	// log to file
    	if (($fp = fopen('upload_log.txt', 'a+')) !== false) {
    		fputs($fp, $log_str);
    		fclose($fp);
    	}
    }
    
    
    /**
     *
     * Delete a directory RECURSIVELY
     * @param string $dir - directory path
     * @link http://php.net/manual/en/function.rmdir.php
     */
    function rrmdir($dir) {
    	if (is_dir($dir)) {
    		$objects = scandir($dir);
    		foreach ($objects as $object) {
    			if ($object != "." && $object != "..") {
    				if (filetype($dir . "/" . $object) == "dir") {
    					$this->rrmdir($dir . "/" . $object);
    				} else {
    					unlink($dir . "/" . $object);
    				}
    			}
    		}
    		reset($objects);
    		rmdir($dir);
    	}
    	return true;
    }
    
    /**
     *
     * Check if all the parts exist, and
     * gather all the parts of the file together
     * @param string $dir - the temporary directory holding all the parts of the file
     * @param string $fileName - the original file name
     * @param string $chunkSize - each chunk size (in bytes)
     * @param string $totalSize - original file size (in bytes)
     */
    function createFileFromChunks($temp_dir, $fileName, $chunkSize, $totalSize) {
    	// count all the parts of this file
    	$total_files = 0;
    	foreach(scandir($temp_dir) as $file) {
    		if (stripos($file, $fileName) !== false) {
    			$total_files++;
    		}
    	}
    	// check that all the parts are present
    	// the size of the last part is between chunkSize and 2*$chunkSize
    	if ($total_files * $chunkSize >=  ($totalSize - $chunkSize + 1)) {
    		global $uploads_dir;
    		// create the final destination file
    		if (($fp = fopen($uploads_dir.'/'.$fileName, 'w')) !== false) {
    			for ($i=1; $i<=$total_files; $i++) {
    				fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
    				//_log('writing chunk '.$i);
    			}
    			fclose($fp);
    		} else {
    			$this->_log('cannot create the destination file');
    			return false;
    		}
    		// rename the temporary directory (to avoid access from other
    		// concurrent chunks uploads) and than delete it
    		if (rename($temp_dir, $temp_dir.'_UNUSED')) {
    			$this->rrmdir($temp_dir.'_UNUSED');
    		} else {
    			$this->rrmdir($temp_dir);
    		}
    		//echo $totalSize."~".basename($fileName);
    		//$fileName_arr = explode('.', $fileName);
    		//echo $totalSize."~".iconv('GB2312', 'UTF-8', $fileName_arr[0]).'.'.$fileName_arr[1];exit; //将字符串的编码从GB2312转到UTF-8
    		echo $totalSize."~".iconv('GB2312', 'UTF-8', $fileName);exit;
    	}
    }
    
    
    function getChunkFilename ($chunkNumber, $identifier,$filename){
    	global $uploads_dir;
    	$temp_dir = $uploads_dir.'/'.$identifier;
    	return  $temp_dir.'/'.$filename.'.part'.$chunkNumber;
    }
    
    
    function changeTimeType($seconds) {
    	if ($seconds > 3600) {
    		$hours = intval($seconds / 3600);
    		$minutes = $seconds % 3600;
    		$time = $hours . ":" . gmstrftime('%M:%S', $minutes);
    	} else {
    		$time = gmstrftime('%H:%M:%S', $seconds);
    	}
    	return $time;
    }
}